package page.transport;

import java.util.List;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.markup.repeater.data.ListDataProvider;

import app.WebApp;
import dao.TransportDao;
import entity.Transport;

public class TransportListPage extends WebPage {

	public TransportListPage() {
		super();

		final TransportDao transportDao = new TransportDao(WebApp.DATA_XML_FOLDER);
		final List<Transport> allTransport = transportDao.getAll();

		add(new DataView<Transport>("table-rows", new ListDataProvider<Transport>(allTransport)) {
			@Override
			protected void populateItem(final Item<Transport> item) {
				final Transport transport = item.getModelObject();
				item.add(new Label("route", transport.getRoute()));
				item.add(new Label("time", transport.getTime()));
				item.add(new Label("type", transport.getType()));
				item.add(new Link("editLink") {
					@Override
					public void onClick() {
						setResponsePage(new TransportPage(transport));
					}
				});

				item.add(new Link("deleteLink") {
					@Override
					public void onClick() {
						transportDao.delete(transport.getId());
						setResponsePage(new TransportListPage());
					}
				});
			}
		});

		add(new Link("createLink") {
			@Override
			public void onClick() {
				setResponsePage(new TransportPage(new Transport()));
			}
		});
		add(new Link("back") {
			@Override
			public void onClick() {
				setResponsePage(new Ways());
			}
		});
	}
}
